package com.roc.web.action.yycard;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.ScanCodeAudit;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblMchtInf;
import com.roc.pojo.TblSettleItem;
import com.roc.service.IYYCardService;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
import com.roc.web.action.settlemgr.SettlementApplyMoneySearchCondition;
@Component
@Scope("prototype")
@Namespace("/scanCodeAudit")
public class ScanCodeAuditAction extends PaginationBaseAction{
	
	private static final long serialVersionUID = 1L;
	private ScanCodeAuditSearchCondition searchCondition;
	private static Logger logger = LogManager.getLogger(ScanCodeAuditAction.class);
	@Autowired
	private IPageService pagenitionService;
	@Autowired
	private IPowerInfoService powerInfo;
	@Autowired
	private IYYCardService yyCardService;
	private ScanCodeAudit scanCodeAudit;
	/**
	 * 初始化页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/yycard/scanCodeAuditList.jsp")
		,@Result(name="error",location="/error.jsp")})
	public String initMerinfoPage(){
		try{
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if(op.getRoleType().equals(Role.SYSTEM.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode())){
				logger.info("页面初始化");
				return SUCCESS;
			}			
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}		
		addActionMessage("当前登录人员无权限访问!");
		return ERROR;
	}
	
	@Action(value="queryMerinfo",results={@Result(name="success",location="/pages/yycard/scanCodeAuditList.jsp")
	,@Result(name="error",location="/error.jsp")})
	public String querySettlementMsgList(){
		try {
			Operators op = (Operators) getSession().getAttribute(
					Constant.SESSION_LOGIN_MERCHANT);
			if(op.getRoleType().equals(Role.SYSTEM.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode())) {
				
				if(searchCondition == null){
					searchCondition = (ScanCodeAuditSearchCondition) getSearchConditionFromSession();
					if(searchCondition == null){
						searchCondition = new ScanCodeAuditSearchCondition();
					}
				}
												
			
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
			dealWithTheListSpecially(searchRsList);
			getRequest().setAttribute("searchRsList",searchRsList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			addActionMessage("统计到["+rows+"]条记录");
			return SUCCESS;
			}
			addActionError("无权限访问!!!");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError("待审核微信扫码申请查询异常");
			return ERROR;
		}
		return ERROR;
	}
	
	
	@Override
	protected void dealWithTheListSpecially(List<Object> searchRsList) {
		List<ScanCodeAudit> audit=new ArrayList<ScanCodeAudit>();
		if(searchRsList != null && searchRsList.size() > 0){
			Iterator<Object> it = searchRsList.iterator();
			while(it.hasNext()){
				Object[] obj = (Object[]) it.next();
				ScanCodeAudit aud=new ScanCodeAudit();
				aud.setMchtCd(Constance.Null2EmptyTrim(obj[0]));
				aud.setName(Constance.Null2EmptyTrim(obj[1]));
				aud.setWxId(Constance.Null2EmptyTrim(obj[2]));
				aud.setWxName(Constance.Null2EmptyTrim(obj[3]));
				aud.setSubid(Constance.Null2EmptyTrim(obj[4]));
				aud.setStatus(Constance.Null2EmptyTrim(obj[5]));
				aud.setDate(Constance.Null2EmptyTrim(obj[6]));
				aud.setCounts(Constance.Null2EmptyTrim(obj[7]));
				audit.add(aud);
			}
		}
		searchRsList.clear();
		searchRsList.addAll(audit);
	}

	/**
	 * 初始化审核页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "initAudit", results = { @Result(name = "success", location = "/pages/yycard/scanCodeAudit.jsp")
		,@Result(name="error",location="/error.jsp")})
	public String initAuditJsp(){
		try{
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if(op.getRoleType().equals(Role.SYSTEM.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode())){
				scanCodeAudit=yyCardService.queryAuditDetail(scanCodeAudit);
				logger.info("页面初始化");
				return SUCCESS;
			}			
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}		
		addActionMessage("当前登录人员无权限访问!");
		return ERROR;
	}
	
	
	/**
	 * 审核页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "auditData", results = { @Result(name = "success", location = "/pages/yycard/scanCodeAuditList.jsp"),
			@Result(name = "failed", location = "/pages/yycard/scanCodeAudit.jsp"),
		@Result(name="error",location="/error.jsp")})
	public String initAudit(){
		try{
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if(op.getRoleType().equals(Role.SYSTEM.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())||
					op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode())){
				Map<String,String> result=yyCardService.updateAudit(scanCodeAudit,op);
				String msg=result.get("message");
				addActionMessage(msg);
				if(result.containsKey("status")){
					return SUCCESS;
				}
				return "failed";
			}			
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}		
		addActionMessage("当前登录人员无权限访问!");
		return ERROR;
	}
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_SCAN_CODE_AUDIT_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (ScanCodeAuditSearchCondition)getSession().getAttribute(SEARCH_SESSION_SCAN_CODE_AUDIT_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		
		return Constance.PAGE_MAX_SIZE+"";
	}

	public ScanCodeAudit getScanCodeAudit() {
		return scanCodeAudit;
	}

	public void setScanCodeAudit(ScanCodeAudit scanCodeAudit) {
		this.scanCodeAudit = scanCodeAudit;
	}

	public ScanCodeAuditSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(ScanCodeAuditSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	
}
